//
//  BalanceTableView.m
//  Cat
//
//  Created by lushitong on 2019/1/9.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "BalanceTableView.h"
#import "HttpUserManager.h"
#import "BalanceCell.h"

@interface BalanceTableView()

@property (nonatomic, strong) NSMutableArray *detailsArray;

@end

@implementation BalanceTableView

- (instancetype)initWithFrame:(CGRect)frame sections:(NSInteger)sections {
	if (self = [super initWithFrame:frame sections:sections]) {
		self.addRefresh = YES;
		self.needLoadMore = YES;
		self.backgroundColor = [UIColor whiteColor];
		self.tableView.backgroundColor = [UIColor whiteColor];
		self.tableView.showsVerticalScrollIndicator = NO;
		self.noFirstAutoRequest = NO;
		self.outerControlLoading = NO;
		self.emTitle = @"无更多明细";
	}
	return self;
}

- (void)sendRequest {
	if (self.pageIndex == 1) {
		[self.detailsArray removeAllObjects];
	}
	[HttpUserManager balanceListIndex:@(self.pageIndex) success:^(NSArray * _Nonnull balanceArray) {
		[self.detailsArray addObjectsFromArray:balanceArray];
		[self p_setbalaceWithArray:balanceArray];
		[self finishLoadData:balanceArray];
		[self.tableView reloadData];
	} failure:^(NSInteger code, NSString *message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)p_setbalaceWithArray:(NSArray *)array {
	QKBaseTableCellModel *meTopCellData = [[QKBaseTableCellModel alloc] init];
	meTopCellData.isDynamic = YES;
	meTopCellData.cellClass = @"BalanceCell";
	meTopCellData.section = 0;
	meTopCellData.cellData = array;
	meTopCellData.cellHeight = kQKFitModule(60);
	[self addTableSectionData:meTopCellData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.detailsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *sectionCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
	if (indexPath.section == 0) {
		BalanceCell *cell = (BalanceCell *)sectionCell;
		cell.cellModelData = [self.detailsArray safeObjectAtIndex:indexPath.row];
	}
	return sectionCell;
}

- (NSMutableArray *)detailsArray {
	if (!_detailsArray) {
		_detailsArray = [[NSMutableArray alloc] init];
	}
	return _detailsArray;
}
@end

